﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;

namespace WpfBeyond
{
    /// <summary>
    /// WindowCalendar.xaml 的交互逻辑
    /// </summary>
    public partial class WindowCalendar : MetroWindow
    {
        /// <summary>
        /// 编辑的时间段
        /// </summary>
        private List<DateTimeRange> range = new List<DateTimeRange>();

        public WindowCalendar(List<DateTimeRange> list)
        {
            InitializeComponent();

            foreach(DateTimeRange r in list)
            {
                range.Add(r);
            }

            grid_range.ItemsSource = range;
        }

        public List<DateTimeRange> Range { get => range; set => range = value; }

        private void btn_ok_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void btn_clear_Click(object sender, RoutedEventArgs e)
        {
            range.Clear();
            grid_range.ItemsSource = null;
            grid_range.ItemsSource = range;
        }

        private async void btn_add_Click(object sender, RoutedEventArgs e)
        {
            if (!dp_begin_time.SelectedDateTime.HasValue)
            {
                await this.ShowMessageAsync("提示", "开始时间未选择！");
                return;
            }

            if( !dp_end_time.SelectedDateTime.HasValue && num_playtime.Value == 0)
            {
                await this.ShowMessageAsync("提示", "结束时间和播放次数不能同时为空！");
                return;
            }

            try
            {

                foreach (DateTime dt in sel_dates.SelectedDates)
                {
                    DateTimeRange r = new DateTimeRange();
                    if (range.Count == 0)
                    {
                        r.Id = 1;
                    }
                    else
                    {
                        r.Id = range[range.Count - 1].Id + 1;
                    }
                    //r.Id = range.Count + 1;
                    r.BeginTime = dt.Add(dp_begin_time.SelectedDateTime.Value.TimeOfDay);
                    if (dp_end_time.SelectedDateTime.HasValue)
                    {
                        r.EndTime = dt.Add(dp_end_time.SelectedDateTime.Value.TimeOfDay);
                    }
                    else
                    {
                        r.PlayTimes = (int)(num_playtime.Value);
                    }

                    range.Add(r);
                }
                grid_range.ItemsSource = null;
                grid_range.ItemsSource = range;
            }
            catch(Exception ex)
            {
                LogHelp.Error(ex.ToString());
            }
        }

        private void DelClick(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            if (btn.Tag == null) return;

            int id = int.Parse(btn.Tag.ToString());

            foreach (DateTimeRange r in range)
            {
                if (id == r.Id)
                {
                    range.Remove(r);
                    grid_range.ItemsSource = null;
                    grid_range.ItemsSource = range;
                    return;
                }
            }
        }

        private void sel_dates_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (Mouse.Captured is System.Windows.Controls.Primitives.CalendarItem)
            {
                Mouse.Capture(null);
            }
        }

    }
}
